<?php
class Response {
	private $__config, $__apiconfig;
	private $listtype;
	private $siteid = 0;
	private $page = 0;
	function __construct($config){
		$this->__config = $config;
		$this->__apiconfig = $this->__config->get('API')->get('APILIST');
		$this->listtype = (isset($_GET['list']) && $_GET['list'] != '')?$_GET['list']:'';
		$this->siteid = (isset($_GET['siteid']) && $_GET['siteid'] != '')? $_GET['siteid'] : '0'; 
		$this->page = (isset($_GET['page']) && $_GET['page'] != '')? $_GET['page'] : '0';
		$this->page = $this->page * 100;  
	}
	
	function __destruct(){
		
	}
	
	public function toString(){
		if (!$this->__apiconfig->get('APIACCES')){
			return 'ACCESSDENIED';
		}
		return $this->execute();
	}
	
	private function execute(){
		$this->set_header();
		return $this->find($this->listtype);
	}
	
	private function set_header(){
		header("Content-type: text/xml");
	}
	
	private function find($api){
		$conf = $this->__apiconfig->get($api);
		if (!$conf->get('APIACCES')){
			return 'ACCESSDENIED';
		}
		$res = "";
		if ($conf->get('APITYPE') == 'FILE'){ 
			$fname = dirname(__FILE__).'/contents/'.$conf->get('APIFILENAME');
			if (file_exists($fname)){
				$content = file_get_contents($fname);
				$res = $this->makexml($conf->get('APIVERSION'),$conf->get('APITYPE'),$conf->get('APIPLAYER'),$content);
			}
		} else { //SITEMAP
			$content = $this->get_sitemaps();
			$res = $this->makexml($conf->get('APIVERSION'),$conf->get('APITYPE'),$conf->get('APIPLAYER'),$content);
		}
		return $res;
	}
		
	private function makexml($apiver, $xmltype, $player, $content){
		$reContent = str_replace('{FILETYPE}',$xmltype, $content);
		$reContent = str_replace('{PLAYER}',$player, $reContent);
	
		$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
			<API>
				<CONTENT VERSION=\"".$apiver."\" TYPE=\"".$xmltype."\" PLAYER=\"".$player."\"><![CDATA[".$reContent."]]></CONTENT>
			</API>";
		return $xml;
	}
	
	private function get_sitemaps(){
		switch ($this->listtype){
			case 'MOVIEAPI' : 
				return $this->get_movie_list();
				break;
			case 'VIDEOAPI':
				return 'VIDEO PAGE';
				break;
			case 'TRAILERAPI':
				return 'TRAILER PAGE';
				break;
			case 'SEARCH':
				return $this->search_result($_GET['q']);
				break;
			default:
				return 'FILE NOT FOUND';
				break;
		}
	}
	
	private function get_movie_list(){
		$res = '';
		$conn = $this->__config->DB->createConnection();
		if ($this->siteid == 0){
			$q = $this->__config->DB->query("SELECT id, url FROM tbl_sitemaps WHERE activate =1");
			while ($d = mysql_fetch_array($q)){
				$name = str_replace('http://','', $d['url']);
				$name = str_replace('www.','', $name);
				$name = substr($name, 0, strpos($name, '/'));
				$res .= $name."[#]".$d['id']."[#]{FILETYPE}[#]".$this->__config->get('SITEURL')."/?mod=api&api_mod=list&list=MOVIEAPI&siteid=".$d["id"]."[#]NON\n";
			}
		} else {
			$q = $this->__config->DB->query("SELECT id, sitemapID, url FROM tbl_urls WHERE active =1 and sitemapID = ".$this->siteid." LIMIT ".$this->page.",100");
			while ($d = mysql_fetch_array($q)){
				$name = basename($d['url']);
				if (strpos($name, '.') !==false){
					$name = substr($name, 0, strpos($name, '.'));
				}
				$name = str_replace('-',' ', $name);
				$name = str_replace('_',' ', $name);
				$res .= $name."[#]".$d['sitemapID']."[#]{FILETYPE}[#]".$d['url']."[#]DIRECT\n";
			}
		}
		$this->__config->DB->destroyConnection();
		return $res;
	}
	
	private function normalize_special_characters( $str )
	{
	
		$unwanted_array = array(    'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
		                            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
		                            'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
		                            'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
		                            'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'İ'=>'I', 'ı'=>'i', 'Ö'=>'O', 'ö'=>'o', 'Ü'=>'U',
									'ü'=>'u', 'Ç'=>'C', 'ç'=>'c', 'Ğ'=>'G', 'ğ'=>'g', 'Ş'=>'S', 'ş'=>'s'   );
		$str = strtr( $str, $unwanted_array );
	
	    return $str;
	}
	
	private function search_result($str){
		$searchstr = str_replace(',',' ',$str);
		$searchstr = str_replace('+',' ',$searchstr);
		$searchstr = str_replace('-',' ',$searchstr);
		$searchstr = str_replace('_',' ',$searchstr);
		$searchstr = str_replace('  ',' ',$searchstr);
		$searchstr = str_replace('  ',' ',$searchstr);
		$searchstr = str_replace("'","",$str);
		$searchstr = trim($searchstr);
		$searchstr = $this->normalize_special_characters($searchstr);
		
		$searchstr = explode(' ', $searchstr);
		$where = "";
		for($i = 0; $i < count($searchstr); $i++){
			$where .= " and u.filename like '%".$searchstr[$i]."%' ";
		}
		$sql = "SELECT s.id sid, s.url sitemapurl, u.id uid, u.url movieurl, u.filename  FROM tbl_sitemaps s ".
				" INNER JOIN tbl_urls u ON s.id = u.sitemapID ".  
				" WHERE s.activate =1 ". $where ." ORDER BY u.sitemapID ASC LIMIT 0,500";
		$conn = $this->__config->DB->createConnection();
		$q = $this->__config->DB->query($sql);
		$sid = 0;
		$res ='';
		while ($d = mysql_fetch_array($q)){
			if ($sid != $d['sid']){
				$name = str_replace('http://','', $d['sitemapurl']);
				$name = str_replace('www.','', $name);
				$name = substr($name, 0, strpos($name, '/'));
				$res .= $name."[#]".$d['sid']."[#]{FILETYPE}[#]".$this->__config->get('SITEURL')."/?mod=api&api_mod=list&list=MOVIEAPI&siteid=".$d["sid"]."[#]NON\n";
				$sid = $d['sid'];
			}
			
			$name = $d['filename'];
			$name = str_replace('-',' ', $name);
			$name = str_replace('_',' ', $name);
			$res .= "	".$name."[#]".$d['sid']."[#]{FILETYPE}[#]".$d['movieurl']."[#]DIRECT\n";
		}
		$this->__config->DB->destroyConnection();
		return $res;
	}
}
?>